<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use app\admin\model\User as UserModel;//用户信息模型
use app\admin\model\Group as GroupModel;

class User extends Permissions
{
    /**
     * 用户信息列表
     * @return [type] [description]
     */
    public function index()
    {
        //实例化用户信息模型
        $model = new UserModel();

        $post = $this->request->param();
        if (isset($post['nickname']) and !empty($post['nickname'])) {
            $where['nickname'] = ['like', '%' . $post['nickname'] . '%'];
        }

        $admin = empty($where) ? $model->order('create_time desc')->paginate(20) : $model->where($where)->order('create_time desc')->paginate(20, false, ['query' => $this->request->param()]);
        $this->assign('admin', $admin);
        $this->assign('post', $post);
        return $this->fetch();
    }


    /**
     * 用户信息的添加及修改
     * @return [type] [description]
     */
    public function publish()
    {
        //获取用户信息id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new UserModel();
        if ($id > 0) {
            //是修改操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['percent', 'require', '代理级别不能为空'],
                    ['goods_id', 'require', '商品不能为空'],
                    ['goods_num', 'require', '商品数量不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                $user = [
                    'percent' => $post['percent'],
                    'role' => 2
                ];
	            if (false == $model->allowField(true)->save($user, ['id' => $id])) {

                    return $this->error('修改失败');
                } else {
	                $data=[
	                    'goods_id'=>$post['goods_id'],
	                    'user_id'=>$id,
	                    'goods_num'=>$post['goods_num'],
	                    'goods_price'=>$post['goods_price'],
                        'create_time'=>time()
                    ];
                    Db::name('user_goods_num')->insert($data);
//                    addlog($model->id);//写入日志
                    return $this->success('修改用户信息成功', 'admin/user/index');
                }
    		} else {
                //非提交操作
                $info = $model->where('id', $id)->find();
                $this->assign('info', $info);
                $goods = Db::name('goods')->select();
                $this->assign('goods', $goods);
                $level = Db::name('level')->select();
                $this->assign('level', $level);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['username', 'require', '用户信息名称不能为空|用户名格式只能是字母、数组、——或_'],
                    ['password', 'require', '密码不能为空'],
                    ['telephone', 'require', '手机号不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                if (false == $model->allowField(true)->save($post)) {
                    return $this->error('添加用户信息失败');
                } else {
//                    addlog($model->id);//写入日志
                    return $this->success('添加用户信息成功', 'admin/user/index');
                }
            } else {
                $goods = Db::name('goods')->select();
                $level = Db::name('level')->select();
                $this->assign('goods', $goods);
                $this->assign('level', $level);
                //非提交操作
                return $this->fetch();
            }
        }
    }

    /**
     * 修改密码
     * @return [type] [description]
     */
    public function edit_password()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;

        if ($id > 0) {
            if ($this->request->isPost()) {
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['password', 'require|confirm', '密码不能为空|两次密码不一致'],
                    ['password_confirm', 'require', '重复密码不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                $User = Db::name('User')->where('id', $id)->find();

                if (password($post['password']) != $User['password']) {
                    if (false == Db::name('User')->where('id', $id)->update(['password' => password($post['password'])])) {
                        return $this->error('修改失败');
                    } else {
//                        addlog();//写入日志
                        return $this->success('修改成功');
                    }
                } else {
                    return $this->error('与原密码一致！');
                }
            } else {
                $id = $id;
                $this->assign('id', $id);
                return $this->fetch();
            }
        } else {
            $id = $id;
            $this->assign('id', $id);
            return $this->fetch();
        }
    }


    /**
     * 用户信息删除
     * @return [type] [description]
     */
    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if (false == Db::name('user')->where('id', $id)->delete()) {
                return $this->error('删除失败');
            } else {
//                addlog($id);//写入日志
                return $this->success('删除成功', 'admin/user/index');
            }
        }
    }

}
